package com.atpudong;
/**
 * 使用同步函数（方法上加关键字synchronized）
 * @author 邓世豪
 *
 */

class ThreadTrain2 implements Runnable{
	private int count=100;
	private Object obj=new Object();

	@Override
	public void run() {
			while(count>0){
				show();
			}
	}
	public synchronized void show(){ 		
		if(count>0){			
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {					
					e.printStackTrace();
				}		
			System.out.println(Thread.currentThread().getName()+",出售第"+(100-count+1)+"张票");
			count--;
		}
		
	}
	
}


public class Threademo2 {
	public static void main(String[] args) {
		ThreadTrain2 threadtrain=new ThreadTrain2();
		Thread thread1=new Thread(threadtrain);
		Thread thread2=new Thread(threadtrain);
		thread1.start();
		thread2.start();
	}

}
